package com.sanchunhui.psc.utils

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.sanchunhui.psc.R
import com.bumptech.glide.request.RequestOptions
import android.graphics.drawable.Drawable
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.transition.Transition



/*
    Glide工具类
 */
object GlideUtils {
    fun loadImage(context: Context, url: String, imageView: ImageView) {

        val myOptions : RequestOptions = RequestOptions()
        myOptions!!.centerCrop()
        Glide.with(context).load(url).apply(myOptions).into(imageView)
    }

    fun loadImageFitCenter(context: Context, url: String, imageView: ImageView) {
        val myOptions : RequestOptions = RequestOptions()
        myOptions!!.fitCenter()
        Glide.with(context).load(url).apply(myOptions).into(imageView)
    }

    /*
        当fragment或者activity失去焦点或者destroyed的时候，Glide会自动停止加载相关资源，确保资源不会被浪费
     */
    fun loadUrlImage(context: Context, url: String, imageView: ImageView){
        val myOptions : RequestOptions = RequestOptions()
        myOptions!!.placeholder(com.sanchunhui.psc.R.drawable.icon_default_user)
        myOptions!!.error(com.sanchunhui.psc.R.drawable.icon_default_user)
        myOptions!!.circleCrop()
        myOptions.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)

//        Glide.with(context).load(url).apply(myOptions).into(imageView)
        Glide.with(context).load(url).apply(myOptions).into(
            object : SimpleTarget<Drawable>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable>?
                ) {
                    imageView.setImageDrawable(resource)
                }
            }
        )
    }
}
